//
//  MineViewController.m
//  Cheirapsis
//
//  Created by wangning on 15/11/2.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "MineViewController.h"
#import "LoginViewController.h"
#import "MyFootViewController.h"
#import "Define.h"
#import "Request.h"
#import "AVOSCloud.h"
#import "UIImageView+WebCache.h"
#import "PersonViewController.h"

@interface MineViewController (){
    UILabel *installedLable;
    UIImageView *installedImage;
    UIImageView *iconImage;
    UIButton *headButton;
    UILabel *headLable;
    UIView *headView;
    AVObject *post;
}

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"个人中心";
    self.view.backgroundColor = ALL233COROL;
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    _dataSource = @[
                   @{
                       @"header": @" ",
                       @"list":@[
                               @{@"title":@"余        额",
                                 @"tupian":@"余额",
                                 @"digital":@"0"},
                               @{@"title":@"优  惠  券",
                                 @"tupian":@"优惠劵",
                                 @"digital":@"0"}
                                ]
                       },
                   @{
                       @"header": @" ",
                       @"list":@[
                               @{@"title":@"我的足迹",
                                 @"tupian":@"足迹",
                                 @"digital":@"0"},
                               @{@"title":@"会员中心",
                                 @"tupian":@"会员",
                                 @"digital":@"0"}
                               ]
                       },
                   @{
                       @"header": @" ",
                       @"list":@[
                               @{@"title":@"账户设置",
                                 @"tupian":@"设置_1",
                                 @"digital":@"0"}
                               ]
                       }
                   ];
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];;
    [delegate.tabBar showTabBar];
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"userName"] != nil){
        headButton.hidden = YES;
        headLable.hidden = NO;
    }else{
        headLable.hidden = YES;
        headButton.hidden = NO;
    }
    if ([AVUser currentUser][@"username"]) {
        AVFile *file = [AVUser currentUser][@"userImg"];
        AVQuery *query = [AVQuery queryWithClassName:@"_File"];
        post = [query getObjectWithId:file.objectId];
        [query getObjectInBackgroundWithId:file.objectId block:^(AVObject *object, NSError *error) {
            post = object;
            [_tableView reloadData];
        }];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 80*F_HEIGHT)];
    headerView.backgroundColor =[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:234/255.0];
    
    
    if (section==0) {
        UIImageView *headImage =[[UIImageView alloc]initWithFrame:CGRectMake(F_WIDTH*30, F_HEIGHT*25, F_WIDTH*60, F_HEIGHT*60)];
        
        
        [headImage sd_setImageWithURL:[NSURL URLWithString:post[@"url"]]];
        
        CALayer *lay  = headImage.layer;
        [lay setMasksToBounds:YES];
        [lay setCornerRadius:30*F_HEIGHT];
        
        headerView.backgroundColor=[UIColor grayColor];
        headButton=[[UIButton alloc]initWithFrame:CGRectMake(F_WIDTH*120, F_HEIGHT*50, F_WIDTH*100, F_HEIGHT*20)];
        [headButton setTitle:@"点击登录" forState:UIControlStateNormal];
        
        headLable = [[UILabel alloc] initWithFrame:CGRectMake(F_WIDTH*120, F_HEIGHT*45, F_WIDTH*100, F_HEIGHT*30)];
        headLable.text = [AVUser currentUser][@"nickName"];
        headLable.font = [UIFont systemFontOfSize:17*F_WIDTH];
        headLable.textColor = [UIColor colorWithRed:159/255.0 green:203/255.0 blue:239/255.0 alpha:1];
        
        if ([[NSUserDefaults standardUserDefaults] valueForKey:@"userName"] != nil){
            headButton.hidden = YES;
            headLable.hidden = NO;
        }else{
            headLable.hidden = YES;
            headButton.hidden = NO;
        }
        headButton.backgroundColor=[UIColor colorWithRed:159/255.0 green:203/255.0 blue:239/255.0 alpha:1];
        [headButton addTarget:self action:@selector(loginIn) forControlEvents:UIControlEventTouchUpInside];
        headView=[[UIView alloc]initWithFrame:CGRectMake(0, F_HEIGHT*110, WIDTH, F_HEIGHT*10)];
        headView.backgroundColor=[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1];
        [headerView addSubview:headView];
        [headerView addSubview:headLable];
        [headerView addSubview:headButton];
        [headerView addSubview:headImage];
    };
    return headerView;

}

-(void)loginIn
{
    LoginViewController *vc =[[LoginViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *dic = [_dataSource objectAtIndex:section];
    NSArray *arr = [dic objectForKey:@"list"];
    return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"fuckQY"];

    
    NSDictionary *dic = [_dataSource objectAtIndex:indexPath.section];
    NSArray *arr = [dic objectForKey:@"list"];
    NSDictionary * dicS = [arr objectAtIndex:indexPath.row];
    UIView *cellLine = [[UIView alloc]initWithFrame:CGRectMake(10, 45*F_HEIGHT, WIDTH, 1)];
    cellLine.backgroundColor =[UIColor  colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1];
    installedLable=[[UILabel alloc]initWithFrame:CGRectMake(F_WIDTH*70, F_HEIGHT*18,  F_WIDTH* 120, F_HEIGHT* 10)];
    installedLable.font=[UIFont systemFontOfSize:15*F_HEIGHT];
    installedLable.text = dicS[@"title"];
#pragma -mark 图标
    iconImage=[[UIImageView alloc]initWithFrame:CGRectMake(F_WIDTH*20, F_HEIGHT*7, F_WIDTH*30, F_HEIGHT*30)];
    iconImage.image=[UIImage imageNamed:dicS[@"tupian"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
#pragma -mark 回箭头
    installedImage=[[UIImageView alloc]initWithFrame:CGRectMake(F_WIDTH*290, F_HEIGHT*10, F_WIDTH*15, F_HEIGHT*20)];
    installedImage.image = [UIImage imageNamed:@"我的回箭头"];
    [cell addSubview:installedImage];
    [cell addSubview:installedLable];
    [cell addSubview:iconImage];
    [cell addSubview:cellLine];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==1) {
        return 10*F_HEIGHT;
    }
    else if (section==2) {
        return 10*F_HEIGHT;
        }
    
    return 120*F_HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45*F_HEIGHT;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1 && indexPath.row == 0) {
        MyFootViewController *myFoot = [[MyFootViewController alloc] init];
        [self.navigationController pushViewController:myFoot animated:YES];
    }
    if (indexPath.section == 2 && indexPath.row == 0) {
        PersonViewController * person = [[PersonViewController alloc] init];
        [self.navigationController pushViewController:person animated:YES];
    }
}

@end
